package com.yinhai.message.manage.register.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yinhai.core.app.ta3.web.controller.BaseController;
import com.yinhai.core.common.ta3.dto.TaParamDto;
import com.yinhai.message.manage.register.bpo.ISmsConfigBpo;
import com.yinhai.message.manage.register.po.SmsConfigPo;
import com.yinhai.message.manage.register.vo.SmsConfigVo;

/**
*短信平台配置控制器
*@author lanyc
*@version created: 2017年4月4日上午11:06:40
*/
@RequestMapping("sms")
@Controller
public class SmsConfigController extends BaseController {
	
	@Resource
	private ISmsConfigBpo scb;
	
	/**
	 * 添加短信平台配置
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("SmsConfigController!toAddSmsConfig.do")
    public String addEmailConfig() throws Exception {
		return "config/sms/smsConfigAdd";
	}
	
	/**
	 * 返回短信配置首页
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("SmsConfigController!smsConfig.do")
    public String queryAllPlatForms() throws Exception {
		List<SmsConfigVo> list = scb.queryAllPlatForms();
		setList("smsgrid", list);
		return "config/sms/smsConfig";
	}
	
	/**
	 * 查询所有短信平台
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("SmsConfigController!queryAllSmsConfig.do")
    public String queryAllSmsConfig() throws Exception {
		List<SmsConfigVo> list = scb.queryAllPlatForms();
		setList("smsgrid", list);
		return JSON;
	}
	
	/**
	 * 保存增加的短信平台
	 * @throws Exception
	 */
	@RequestMapping("SmsConfigController!saveSmsConfig.do")
	public String saveSmsConfig(){
		TaParamDto dto = getTaDto();
		SmsConfigPo scp = new SmsConfigPo();
		scp.setAppName(dto.getAsString("appName"));
		scp.setPlatformName(dto.getAsString("platformName"));
		scp.setMainAccount(dto.getAsString("mainAccount"));
		scp.setPassword(dto.getAsString("password"));
		scp.setServerIp(dto.getAsString("serverIp"));
		scp.setServerPort(Integer.parseInt(dto.getAsString("serverPort")));
		String defaultPlatform = dto.getAsString("defaultPlatform");
		if (defaultPlatform!=null&&defaultPlatform=="dp") {
			scp.setDefaultPlatform(1);
		}else{
			scp.setDefaultPlatform(0);
		}
		scb.addPlatForm(scp);
		return JSON;
	}
	
	/**
	 * 打开编辑短信平台页面
	 * @return
	 * @throws UnsupportedEncodingException 
	 * @throws Exception
	 */
	@RequestMapping("SmsConfigController!toEditSmsConfig.do")
	public String toEditSmsConfig(HttpServletRequest request) throws UnsupportedEncodingException{
		request.setCharacterEncoding("utf-8");
		TaParamDto dto = getTaDto();
		String platformName = dto.getAsString("platformName");
		SmsConfigVo scv = scb.queryPlatformByName(platformName);
		setData("platformName", scv.getPlatformName());
		setData("serverIp",scv.getServerIp());
		setData("serverPort", scv.getServerPort());
		setData("appName",scv.getAppName());
		setData("mainAccount",scv.getMainAccount());
		if(scv!=null&&scv.getDefaultPlatform()==1){
			setData("defaultPlatform","dp");
		}
		return "config/sms/smsConfigEdit";
	}
	
}
